package A_review.re2_15;

import binaryTree.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Class589 {

    class Node {
        public int val;
        public List<Node> children;
        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    public  void preOrderTraver(TreeNode root, ArrayList list){
        if (root==null){
            return;
        }
        preOrderTraver(root.left,list);
        list.add(root.val);
        preOrderTraver(root.right,list);
    }

    public  List<Integer>  feidiguiPreOrder(TreeNode root){
        ArrayList<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root==null){
            return  ans;
        }
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            ans.add(node.val);
            if (node.right!=null){
                stack.add(node.right);
            }
            if (node.left!=null){
                stack.add(node.left);
            }
        }
        return ans;
    }




    ArrayList<Integer> ans;
    public List<Integer> preorder(Node root) {
        ans=new ArrayList<>();
        if (root==null){
            return ans;
        }
        preorder(root,ans);
        return  ans;
    }
    public void preorder(Node root,List<Integer> ans) {
        if (root==null){
            return  ;
        }
        ans.add(root.val);
        for (Node ch :root.children) {
            preorder(ch,ans);
        }
    }



    }
